package jgl.vajra.management.part.home.activity;

import android.widget.ImageView;
import android.widget.TextView;

import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.ListeningSettingBean;
import jgl.vajra.management.bean.ListeningSettingConditionBean;
import jgl.vajra.management.databinding.ActivityListeningSettingBinding;
import jgl.vajra.management.event.ListeningMyCarEvent;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.home.contract.ListeningSettingContract;
import jgl.vajra.management.part.home.viewModel.ListeningSettingViewModel;
import jgl.vajra.management.widget.manager.SPManager;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(ListeningSettingViewModel.class)
public class ListeningSettingActivity extends BaseMVVMActivity<ListeningSettingViewModel, ActivityListeningSettingBinding> implements ListeningSettingContract.View {

    private boolean isSwitch;
    private List<String> carList = new ArrayList<>();
    private List<ListeningSettingConditionBean> conditionList = new ArrayList<>();

    private boolean isClick1 = true;
    private boolean isClick2 = true;
    private boolean isClick3 = true;
    private boolean isClick4 = true;
    private boolean isClick5 = true;
    private boolean isClick6 = true;
    private boolean isClick7 = true;
    private boolean isClick8 = true;

    private int leftProgress1 = 6;
    private int leftProgress2 = 12;
    private int leftProgress3 = 19;
    private int leftProgress4 = 0;
    private int leftProgress5 = 0;
    private int leftProgress6 = 10;
    private int leftProgress7 = 20;
    private int leftProgress8 = 30;

    private int rightProgress1 = 12;
    private int rightProgress2 = 19;
    private int rightProgress3 = 23;
    private int rightProgress4 = 6;
    private int rightProgress5 = 10;
    private int rightProgress6 = 20;
    private int rightProgress7 = 30;
    private int rightProgress8 = 40;

    @Override
    public int getLayoutId() {
        return R.layout.activity_listening_setting;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        isSwitch = SPManager.MineData.getVoicePlayback();
        mBinding.ivSwitch.setImageResource(isSwitch ? R.drawable.ic_setting_open : R.drawable.ic_setting_shut);


        setRangeSeekBar(1, mBinding.sbRange1, mBinding.tv1, mBinding.iv1, isClick1, 6, 12, leftProgress1, rightProgress1, true);
        setRangeSeekBar(2, mBinding.sbRange2, mBinding.tv2, mBinding.iv2, isClick2, 12, 19, leftProgress2, rightProgress2, true);
        setRangeSeekBar(3, mBinding.sbRange3, mBinding.tv3, mBinding.iv3, isClick3, 19, 23, leftProgress3, rightProgress3, true);
        setRangeSeekBar(4, mBinding.sbRange4, mBinding.tv4, mBinding.iv4, isClick4, 0, 6, leftProgress4, rightProgress4, true);
        setRangeSeekBar(5, mBinding.sbRange5, mBinding.tv5, mBinding.iv5, isClick5, 0, 10, leftProgress5, rightProgress5, false);
        setRangeSeekBar(6, mBinding.sbRange6, mBinding.tv6, mBinding.iv6, isClick6, 10, 20, leftProgress6, rightProgress6, false);
        setRangeSeekBar(7, mBinding.sbRange7, mBinding.tv7, mBinding.iv7, isClick7, 20, 30, leftProgress7, rightProgress7, false);
        setRangeSeekBar(8, mBinding.sbRange8, mBinding.tv8, mBinding.iv8, isClick8, 30, 40, leftProgress8, rightProgress8, false);

        requestNetData();
    }

    public void requestNetData() {

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);//随机数

        mViewModel.getDetermineListeningSetting(map);
    }

    public void setRangeSeekBar(int position, RangeSeekBar sbRange, TextView textView, ImageView imageView, boolean ivSelected, float leftRange, float rightRange, float leftProgress, float rightProgress, boolean isTime) {
        imageView.setSelected(ivSelected);
        sbRange.setRange(leftRange, rightRange);
        sbRange.setProgress(leftProgress, rightProgress);

        if (isTime) {
            setSettingTimeData(textView, (int) leftProgress, (int) rightProgress);
        } else {
            textView.setText((int) leftProgress + "～" + (int) rightProgress + "公里");
        }
        sbRange.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar rangeSeekBar, float leftValue, float rightValue, boolean isFromUser) {
                if (isTime) {
                    setSettingTimeData(textView, (int) leftValue, (int) rightValue);
                } else {
                    textView.setText((int) leftValue + "～" + (int) rightValue + "公里");
                }

                switch (position) {
                    case 1:
                        leftProgress1 = (int) leftValue;
                        rightProgress1 = (int) rightValue;
                        break;
                    case 2:
                        leftProgress2 = (int) leftValue;
                        rightProgress2 = (int) rightValue;
                        break;
                    case 3:
                        leftProgress3 = (int) leftValue;
                        rightProgress3 = (int) rightValue;
                        break;
                    case 4:
                        leftProgress4 = (int) leftValue;
                        rightProgress4 = (int) rightValue;
                        break;
                    case 5:
                        leftProgress5 = (int) leftValue;
                        rightProgress5 = (int) rightValue;
                        break;
                    case 6:
                        leftProgress6 = (int) leftValue;
                        rightProgress6 = (int) rightValue;
                        break;
                    case 7:
                        leftProgress7 = (int) leftValue;
                        rightProgress7 = (int) rightValue;
                        break;
                    case 8:
                        leftProgress8 = (int) leftValue;
                        rightProgress8 = (int) rightValue;
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar rangeSeekBar, boolean b) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar rangeSeekBar, boolean b) {

            }
        });
    }


    public void setSettingTimeData(TextView textView, int leftTime, int rightTime) {
        String timeStart;
        String timeEnd;
        if (leftTime < 10) {
            timeStart = "0" + leftTime + ":00";
        } else {
            timeStart = leftTime + ":00";
        }
        if (rightTime < 10) {
            timeEnd = "0" + rightTime + ":00";
        } else {
            timeEnd = rightTime + ":00";
        }
        textView.setText(timeStart + "～" + timeEnd);
    }

    //语言播报
    @Override
    public void onVoiceAnnouncements() {
        isSwitch = !isSwitch;
        SPManager.MineData.saveVoicePlayback(isSwitch);
        mBinding.ivSwitch.setImageResource(isSwitch ? R.drawable.ic_setting_open : R.drawable.ic_setting_shut);
    }

    //全部车型
    @Override
    public void onAllModels() {
        mBinding.tvMyModels.setText("选择我的豪车");
        carList.clear();
        mBinding.tvAllModels.setBackgroundResource(R.drawable.shape_color_0f2cbb_4dp_edge);
        mBinding.tvMyModels.setBackgroundResource(R.drawable.shape_color_eeeeee_4dp);
    }

    //选择我的豪车
    @Override
    public void onMyModels() {
    }

    @Override
    public void onImage1() {
        isClick1 = !isClick1;
        mBinding.iv1.setSelected(isClick1);
    }

    @Override
    public void onImage2() {
        isClick2 = !isClick2;
        mBinding.iv2.setSelected(isClick2);
    }

    @Override
    public void onImage3() {
        isClick3 = !isClick3;
        mBinding.iv3.setSelected(isClick3);
    }

    @Override
    public void onImage4() {
        isClick4 = !isClick4;
        mBinding.iv4.setSelected(isClick4);
    }

    @Override
    public void onImage5() {
        isClick5 = !isClick5;
        mBinding.iv5.setSelected(isClick5);
    }

    @Override
    public void onImage6() {
        isClick6 = !isClick6;
        mBinding.iv6.setSelected(isClick6);
    }

    @Override
    public void onImage7() {
        isClick7 = !isClick7;
        mBinding.iv7.setSelected(isClick7);
    }

    @Override
    public void onImage8() {
        isClick8 = !isClick8;
        mBinding.iv8.setSelected(isClick8);
    }

    //重置
    @Override
    public void onReset() {
        mBinding.sbRange1.setProgress(6, 12);
        mBinding.sbRange2.setProgress(12, 19);
        mBinding.sbRange3.setProgress(19, 23);
        mBinding.sbRange4.setProgress(0, 6);

        mBinding.sbRange5.setProgress(0, 10);
        mBinding.sbRange6.setProgress(10, 20);
        mBinding.sbRange7.setProgress(20, 30);
        mBinding.sbRange8.setProgress(30, 40);
        setDefaultData();
        setIvSelected();

        carList.clear();
        mBinding.tvMyModels.setText("选择我的豪车");
        mBinding.tvAllModels.setBackgroundResource(R.drawable.shape_color_0f2cbb_4dp_edge);
        mBinding.tvMyModels.setBackgroundResource(R.drawable.shape_color_eeeeee_4dp);


    }

    private void setIvSelected() {
        mBinding.iv1.setSelected(isClick1);
        mBinding.iv2.setSelected(isClick2);
        mBinding.iv3.setSelected(isClick3);
        mBinding.iv4.setSelected(isClick4);
        mBinding.iv5.setSelected(isClick5);
        mBinding.iv6.setSelected(isClick6);
        mBinding.iv7.setSelected(isClick7);
        mBinding.iv8.setSelected(isClick8);
    }

    //完成
    @Override
    public void onDetermine() {
        conditionList.clear();
        conditionList.add(new ListeningSettingConditionBean(1, isClick1, leftProgress1, rightProgress1));
        conditionList.add(new ListeningSettingConditionBean(2, isClick2, leftProgress2, rightProgress2));
        conditionList.add(new ListeningSettingConditionBean(3, isClick3, leftProgress3, rightProgress3));
        conditionList.add(new ListeningSettingConditionBean(4, isClick4, leftProgress4, rightProgress4));
        conditionList.add(new ListeningSettingConditionBean(5, isClick5, leftProgress5, rightProgress5));
        conditionList.add(new ListeningSettingConditionBean(6, isClick6, leftProgress6, rightProgress6));
        conditionList.add(new ListeningSettingConditionBean(7, isClick7, leftProgress7, rightProgress7));
        conditionList.add(new ListeningSettingConditionBean(8, isClick8, leftProgress8, rightProgress8));
        Map<String, Object> map = new HashMap<>();
        map.put("carList", carList);
        map.put("conditionList", conditionList);
        mViewModel.determineListeningSetting(map);
    }

    @Override
    public void returnDetermineListeningSetting(String s) {
        ToastUtils.showShort(s);
        finish();
    }

    @Override
    public void returnSettingData(ListeningSettingBean data) {
        carList.clear();
        carList.addAll(data.carList);
        if (carList.size() == 0) {
            mBinding.tvMyModels.setText("选择我的豪车");
            mBinding.tvAllModels.setBackgroundResource(R.drawable.shape_color_0f2cbb_4dp_edge);
            mBinding.tvMyModels.setBackgroundResource(R.drawable.shape_color_eeeeee_4dp);
        } else {
            mBinding.tvMyModels.setText("已选" + carList.size() + "部豪车");
            mBinding.tvMyModels.setBackgroundResource(R.drawable.shape_color_0f2cbb_4dp_edge);
            mBinding.tvAllModels.setBackgroundResource(R.drawable.shape_color_eeeeee_4dp);
        }

        for (int i = 0; i < data.conditionList.size(); i++) {

            switch (data.conditionList.get(i).tag) {
                case 1:
                    isClick1 = data.conditionList.get(i).isisClick;
                    leftProgress1 = data.conditionList.get(i).leftProgress;
                    rightProgress1 = data.conditionList.get(i).rightProgress;
                    break;
                case 2:
                    isClick2 = data.conditionList.get(i).isisClick;
                    leftProgress2 = data.conditionList.get(i).leftProgress;
                    rightProgress2 = data.conditionList.get(i).rightProgress;
                    break;
                case 3:
                    isClick3 = data.conditionList.get(i).isisClick;
                    leftProgress3 = data.conditionList.get(i).leftProgress;
                    rightProgress3 = data.conditionList.get(i).rightProgress;
                    break;
                case 4:
                    isClick4 = data.conditionList.get(i).isisClick;
                    leftProgress4 = data.conditionList.get(i).leftProgress;
                    rightProgress4 = data.conditionList.get(i).rightProgress;
                    break;
                case 5:
                    isClick5 = data.conditionList.get(i).isisClick;
                    leftProgress5 = data.conditionList.get(i).leftProgress;
                    rightProgress5 = data.conditionList.get(i).rightProgress;
                    break;
                case 6:
                    isClick6 = data.conditionList.get(i).isisClick;
                    leftProgress6 = data.conditionList.get(i).leftProgress;
                    rightProgress6 = data.conditionList.get(i).rightProgress;
                    break;
                case 7:
                    isClick7 = data.conditionList.get(i).isisClick;
                    leftProgress7 = data.conditionList.get(i).leftProgress;
                    rightProgress7 = data.conditionList.get(i).rightProgress;
                    break;
                case 8:
                    isClick8 = data.conditionList.get(i).isisClick;
                    leftProgress8 = data.conditionList.get(i).leftProgress;
                    rightProgress8 = data.conditionList.get(i).rightProgress;
                    break;


            }
        }
        setIvSelected();
        mBinding.sbRange1.setProgress(leftProgress1, rightProgress1);
        mBinding.sbRange2.setProgress(leftProgress2, rightProgress2);
        mBinding.sbRange3.setProgress(leftProgress3, rightProgress3);
        mBinding.sbRange4.setProgress(leftProgress4, rightProgress4);

        mBinding.sbRange5.setProgress(leftProgress5, rightProgress5);
        mBinding.sbRange6.setProgress(leftProgress6, rightProgress6);
        mBinding.sbRange7.setProgress(leftProgress7, rightProgress7);
        mBinding.sbRange8.setProgress(leftProgress8, rightProgress8);

    }


    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onListeningMyCarEvent(ListeningMyCarEvent event) {
        carList.clear();
        carList.addAll(event.carList);
        if (event.carList.size() == 0) {
            mBinding.tvMyModels.setText("选择我的豪车");
        } else {
            mBinding.tvMyModels.setText("已选" + event.carList.size() + "部豪车");
        }
        mBinding.tvMyModels.setBackgroundResource(R.drawable.shape_color_0f2cbb_4dp_edge);
        mBinding.tvAllModels.setBackgroundResource(R.drawable.shape_color_eeeeee_4dp);
    }

    public void setDefaultData() {
        isClick1 = true;
        isClick2 = true;
        isClick3 = true;
        isClick4 = true;
        isClick5 = true;
        isClick6 = true;
        isClick7 = true;
        isClick8 = true;
        leftProgress1 = 6;
        leftProgress2 = 12;
        leftProgress3 = 19;
        leftProgress4 = 0;
        leftProgress5 = 0;
        leftProgress6 = 10;
        leftProgress7 = 20;
        leftProgress8 = 30;
        rightProgress1 = 12;
        rightProgress2 = 19;
        rightProgress3 = 23;
        rightProgress4 = 6;
        rightProgress5 = 10;
        rightProgress6 = 20;
        rightProgress7 = 30;
        rightProgress8 = 40;
    }
}
